package com.example.controller;

import com.example.common.Result;
import com.example.entity.EmployeeDetail;
import com.example.service.EmployeeDetailService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 员工详情控制器
 */
@RestController
@RequestMapping("/employeeDetail")
public class EmployeeDetailController {

    @Resource
    private EmployeeDetailService employeeDetailService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody EmployeeDetail employeeDetail) {
        employeeDetailService.add(employeeDetail);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        employeeDetailService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        employeeDetailService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result update(@RequestBody EmployeeDetail employeeDetail) {
        employeeDetailService.updateById(employeeDetail);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        EmployeeDetail employeeDetail = employeeDetailService.selectById(id);
        return Result.success(employeeDetail);
    }
    
    /**
     * 根据员工ID查询
     */
    @GetMapping("/selectByEmployeeId/{employeeId}")
    public Result selectByEmployeeId(@PathVariable Integer employeeId) {
        EmployeeDetail employeeDetail = employeeDetailService.selectByEmployeeId(employeeId);
        return Result.success(employeeDetail);
    }

    /**
     * 查询全部
     */
    @GetMapping("/selectAll")
    public Result selectAll(EmployeeDetail employeeDetail) {
        List<EmployeeDetail> list = employeeDetailService.selectAll(employeeDetail);
        return Result.success(list);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(EmployeeDetail employeeDetail,
                           @RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "10") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<EmployeeDetail> list = employeeDetailService.selectAll(employeeDetail);
        PageInfo<EmployeeDetail> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }
}